<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>添加管理员-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
    <meta HTTP-EQUIV="pragma" CONTENT="no-cache">
    <meta HTTP-EQUIV="Cache-Control" CONTENT="no-store, must-revalidate">
    <meta HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
    <meta HTTP-EQUIV="expires" CONTENT="0">

    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../Admin/static/css/font.css">
    <link rel="stylesheet" href="../Admin/static/css/weadmin.css">
    <link rel="stylesheet" href="../src/css/layui.css" media="all">
    <script src="../js/des.js"></script>
    <script src="../Admin/lib/layui/layui.js" charset="utf-8"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>
<div class="weadmin-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
                <span class="we-red">*</span>登录名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="username" name="username" required="" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="we-red">*</span>将会成为您唯一的登入名
            </div>
        </div>
        <div class="layui-form-item">
            <label for="tid" class="layui-form-label">
                教师号
            </label>
            <div class="layui-input-inline">
                <input type="number" id="tid" name="tid" required="" lay-verify="tid"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                默认为0
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_email" class="layui-form-label">
                <span class="we-red">*</span>邮箱
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_email" name="email" required="" lay-verify="email"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="we-red">*</span>用于找回账号
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span class="we-red">*</span>角色</label>
            <div class="layui-input-block">
                <input type="radio" name="role" value="0" title="管理员" checked>
                <input type="radio" name="role" value="1" title="超级管理员">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_pass" class="layui-form-label">
                <span class="we-red">*</span>密码
            </label>
            <div class="layui-input-inline">
                <input type="password" id="L_pass" name="pass" required="" lay-verify="pass"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                6到16个字符
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
                <span class="we-red">*</span>确认密码
            </label>
            <div class="layui-input-inline">
                <input type="password" id="L_repass" name="repass" required="" lay-verify="repass"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="question" class="layui-form-label">
                <span class="we-red">*</span>安全问题
            </label>
            <div class="layui-input-inline">
                <input type="text" id="question" name="question" required="" lay-verify="question"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="we-red">*</span>用于找回账号
            </div>
        </div>
        <div class="layui-form-item">
            <label for="answer" class="layui-form-label">
                <span class="we-red">*</span>答案
            </label>
            <div class="layui-input-inline">
                <input type="text" id="answer" name="answer" required="" lay-verify="answer"
                       autocomplete="off" class="layui-input">
            </div>

        </div>


        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <a class="layui-btn" lay-filter="reg" lay-submit>
                增加
            </a>
            <a class="layui-btn" href="javascript:;" onclick="closePage()">
                关闭
            </a>
        </div>
    </form>
</div>
<script type="text/javascript">
    function closePage() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }


    layui.use(['form', 'layer', 'admin', 'jquery'], function () {
        var form = layui.form,
            $ = layui.$,
            admin = layui.admin,
            layer = layui.layer;
        //自定义验证规则
        form.verify({
            nikename: function (value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            }
            , pass: [/(.+){6,12}$/, '密码必须6到12位']
            , repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(reg)', function (data) {
            console.log(data.field);
            //发异步，把数据提交给php
            if (data.field.pass != data.field.repass) {
                layer.msg("两次密码不相同");
                return false;

            }
            if (data.field.tid.length < 1) {
                data.field.tid = 0;
            }
            var newTable = {
                "user": data.field.username,
                'tid': data.field.tid,
                'level': data.field.role,
                "password": strEnc(data.field.pass, 'amoxu', 'amoxu', 'amoxu'),
                "mail": data.field.email,
                "question": data.field.question,
                "answer": data.field.answer
            };
            $.ajax({
                type: 'post'
                , async: false
                , url: "/user/add"
                , data: newTable
                , dataType: 'json'
                , contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
                , success: function (res) {
                    if (0 == res['status']) {
                        layer.msg(res['msg'] + "<br>2秒后自动关闭页面");
                        setTimeout("closePage();", 2000)
                    } else {
                        layer.msg(res['msg']);
                    }
                    return false;
                }
                , error: function (res) {
                    layer.msg("错误码：" + res.status);
                }
                , beforeSend: function (res) {
                    layer.load(2, {time: 10 * 1000});
                }
                , complete: function (res) {
                    layer.closeAll('loading');
                }
            });

            return false;
        });
    });
</script>
</body>

</html>